import java.awt.*;
import java.awt.geom.AffineTransform;
import javax.swing.ImageIcon;

/**
 * @author  8100556811
 */
public class SpriteTest2 {

	public static void main(String[] args) {
		SpriteTest2 test = new SpriteTest2();
		test.run();

	}
	  private static final DisplayMode POSSIBLE_MODES[] ={
	    	new DisplayMode(800,600,16,0),
	    	new DisplayMode(800,600,24,0),
	    	new DisplayMode(800,600,16,0),
	    	new DisplayMode(1024,768,32,0),
	    	new DisplayMode(1024,768,24,0)    	
	    };
	    
	    
	    private static final long DEMO_TIME = 5000;
	    private static final long FADE_TIME = 1000;
	    private static final int NUM_SPRITES = 9;
	    
	    /**
		 * @uml.property  name="screen"
		 * @uml.associationEnd  
		 */
	    private ScreenManager screen;
	    private Image bgImage;
	    /**
		 * @uml.property  name="sprites"
		 * @uml.associationEnd  multiplicity="(0 -1)"
		 */
	    private Sprite sprites[];
	    
	    public void loadImages() {
	        // load images
	        bgImage = loadImage("images/background.jpg");
	        Image player1 = loadImage("images/player1.png");
	        Image player2 = loadImage("images/player2.png");
	        Image player3 = loadImage("images/player3.png");

	        // create sprite
	        sprites = new Sprite[NUM_SPRITES];
	        for (int i=0;i<NUM_SPRITES; i++)
	        {
	        	Animation anim = new Animation();
		        anim.addFrame(player1, 250);
		        anim.addFrame(player2, 150);
		        anim.addFrame(player1, 150);
		        anim.addFrame(player2, 150);
		        anim.addFrame(player3, 200);
		        anim.addFrame(player2, 150);
		        sprites[i] = new Sprite(anim);
		        
		        //select random starting location
		        sprites[i].setX((float)Math.random()* (screen.getWidth() - sprites[i].getWidth()));
		        sprites[i].setY((float)Math.random()* (screen.getHeight() - sprites[i].getHeight()));
		        //Select random velocity		        
		        sprites[i].setVelocityX((float)Math.random()- 0.5f);
		        sprites[i].setVelocityY((float)Math.random()- 0.5f);
		        
	        }
	        
	    }
	    
	    private Image loadImage(String fileName) {
	        return new ImageIcon(fileName).getImage();
	    }
	    
	    public void run() {
	        screen = new ScreenManager();
	        try {
	        	DisplayMode displayMode = screen.findFirstCompatibleMode(POSSIBLE_MODES);
	            screen.setFullScreen(displayMode);
	            loadImages();
	            animationLoop();
	        }
	        finally {
	            screen.restoreScreen();
	        }
	    }
	    
	    
	    public void animationLoop() {
	        long startTime = System.currentTimeMillis();
	        long currTime = startTime;

	        while (currTime - startTime < DEMO_TIME) {
	            long elapsedTime =
	                System.currentTimeMillis() - currTime;
	            currTime += elapsedTime;

	            // update sprite
	            update(elapsedTime);

	            // draw AND UPDATE  screen
	            Graphics2D g = screen.getGraphics();
	            draw(g);            
	            g.dispose();
	            screen.update();

	            // take a nap
	            try {
	                Thread.sleep(20);
	            }
	            catch (InterruptedException ex) { }
	        }

	    }
	    
	    public void drawFade(Graphics2D g, long currTime)
	    {
	    	long time=0;
	    	
	    	if(currTime<= FADE_TIME)
	    	{
	    		time=FADE_TIME - currTime;	    		
	    	}
	    	else if (currTime > DEMO_TIME -FADE_TIME)
	    	{
	    		time=FADE_TIME -DEMO_TIME +currTime;
	    		
	    	}
	    	else
	    	{
	    		return;
	    	}
	    	
	    	byte numBars=8;
	    	int barHeight = screen.getHeight() / numBars;
	    	int blackHeight= (int) (time * barHeight /FADE_TIME);
	    	g.setColor(Color.black);
	    	
	    	for (int i=0; i< numBars;i++)
	    	{
	    		int y = i*barHeight + (barHeight -blackHeight)/2;
	    		g.fillRect(0, y, screen.getWidth(),blackHeight);
	    	}
	    	
	    }

	    
	    public void update(long elapsedTime)
	    {
	    	
	    	for (int i=0; i<NUM_SPRITES;i++)
	    	{
	    		Sprite sprite = sprites[i];
	    	
	    		//check  sprite bounds
		    	if (sprite.getX()<0)
		    	{
		    		sprite.setVelocityX(Math.abs(sprite.getVelocityX()));
		    	}
		    	else if (sprite.getX() + sprite.getWidth()>= screen.getWidth())
		    	{
		    		sprite.setVelocityX(-Math.abs(sprite.getVelocityX()));
		    	}
		    	
		    	if (sprite.getY()<0)
		    	{
		    		sprite.setVelocityY(Math.abs(sprite.getVelocityY()));
		    	}
		    	else if (sprite.getY() + sprite.getHeight()>= screen.getHeight())
		    	{
		    		sprite.setVelocityY(-Math.abs(sprite.getVelocityY()));
		    	}
		    	
		    	//UPDATE SPRITE
		    	sprite.update(elapsedTime);

	    	}
	    		    	
	    }
	    
	    public void draw(Graphics2D g) {
	        // draw background
	        g.drawImage(bgImage, 0, 0, null);

	        AffineTransform transform = new AffineTransform();
	        
	        for (int i=0; i<NUM_SPRITES; i++)
	        {
	        	Sprite sprite = sprites[i];
	        	//tanslate to sprite
	        	transform.setToTranslation(sprite.getX(),sprite.getY());
	        	
	        	//if the sprite moving left, flip image
	        	if (sprite.getVelocityX()<0)
	        	{
	        		transform.scale(-1,1);
	        		transform.translate(-sprite.getWidth(), 0);
	        	}
	        	
	        	//drawiamge
	        	g.drawImage(sprite.getImage(),transform,null);
	        	
	        }
	     	        
	    }
	    
}
